#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/15 11:14
# @Author  : 王凯
# @File    : guizhou_illegal.py
# @Project : spider-man
import json

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider


class GuizhouIllegalSpider(BaseTaxIllegalSpider):
    name: str = "guizhou_illegal"
    province: str = "贵州"
    url: str = "https://guizhou.chinatax.gov.cn/zdsswfsxajxxgbl/index.html"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.guizhou.chinatax.gov.cn:8443"})
        url = "https://guizhou.chinatax.gov.cn/irs/front/list"
        data = {
            "pageNo": "1",
            "pageSize": "5000",
            "tenantId": "71",
            "tableName": "t_17e6b2c59ad",
            "searchFields": [{"fieldName": "f_2022118461091", "searchWord": ""}],
            "sorts": [{"sortField": "save_time", "sortOrder": "DESC"}],
            "customFilter": {
                "operator": "or",
                "properties": [{"property": "f_202211853800", "operator": "eq", "value": "5735176"}],
            },
            "channelId": "5735176",
            "isPage": True,
        }
        yield self.FormRequest(
            url,
            method="POST",
            body=json.dumps(data, separators=(",", ":")),
            headers={"Content-Type": "application/json"},
            callback=self.parse,
        )

    def parse(self, response, **kwargs):
        data_list = response.json().get("data").get("list", [])
        mapping_dict = {
            "f_2022118461091": "company_name",
            "f_2022118237203": "taxpayer_id",
            "f_2022118305177": "org_code",
            "f_2022118615143": "address",
            "f_202211845:f_2022118164183:f_2022118888238": "legal_representative",
            "f_2022210398099": "illegal_status",
            "f_2022210646272": "illegal_facts",
            "f_2022210441122": "basis_and_punishment",
        }
        for data in data_list:
            item = NetTaxIllegalProItem()
            for i in mapping_dict.keys():
                if ":" in i:
                    value_list = []
                    for i_name in i.split(":"):
                        if data.get(i_name):
                            value_list.append(data.get(i_name))
                    setattr(item, mapping_dict.get(i), " ".join(value_list) if value_list else None)
                else:
                    setattr(item, mapping_dict.get(i), None if data.get(i) == "--" else data.get(i))
            item.province = self.province
            item.year = data["save_time"][:10].replace("-", "")
            yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "guizhou_illegal"])
